package cn.jekun.web.shop.service;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.NotNull;
import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.component.core.bean.gen.RsUserShop;
import cn.jekun.component.core.service.ComponentCoreService;
import cn.jekun.component.core.service.gen.RsUserService;
import cn.jekun.component.core.service.gen.RsUserShopService;
import cn.jekun.web.shop.bean.UserShop;

@Service(ShopUserService.BEAN_NAME)
public class ShopUserService extends ComponentCoreService {

	public static final String BEAN_NAME = "cn.jekun.web.shop.service.ShopUserService";
	public static final String NAMESPACE="ACTION_USER_SHOP";
	@Override
	public String getNamespace() {
		return NAMESPACE;
	}
    @Resource(name=RsUserService.BEAN_NAME)
	RsUserService userService;
    @Resource(name=RsUserShopService.BEAN_NAME)
    RsUserShopService userShopService;
	/**
	 * 获取商店店员的基本信息
	 * @param shopId
	 * @param page
	 * @return
	 */
	public List<UserShop> getUserShopList(@RsUser.AnnoRealName String realName,@RsUser.AnnoUserName String username,@Security String mobile,String beginDate,String endDate,@RsUserShop.AnnoStaffId String staffId,@RsUser.AnnoUserId String userId,@ErpShop.AnnoShopId String shopId,@NotNull Page page){
		   String SQL ="";
		   if(shopId!=null && shopId.toString().length()>0){
				SQL +=" and a.SHOP_ID = '"+shopId.toString()+"'"; 
			}if("".equals(beginDate) && !"".equals(endDate)){
				SQL +=" and a.create_time"
						+ "<= to_date('"+endDate.toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
			}if(!"".equals(beginDate) && "".equals(endDate)){
				SQL +=" and a.create_time"
						+ ">= to_date('"+beginDate.toString().trim()+"','yyyy/MM/dd hh24:mi:ss')";
			}if(!"".equals(beginDate) && beginDate!=null 
					&& !"".equals(endDate) && endDate!=null){
				SQL +=" and a.create_time >= to_date('"+beginDate.toString().trim()+"','yyyy/MM/dd hh24:mi:ss')"
						+ " and a.create_time <= to_date('"+endDate.toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')"; 
			}if(username!=null && username.toString().length()>0 ){
				SQL +=" and b.user_name like '%"+username.toString()+"%'"; 	
			}if(mobile!=null && mobile.toString().length()>0 ){
				SQL +=" and b.mobile like '%"+mobile.toString()+"%'"; 	
			}if(staffId!=null && staffId.toString().length()>0 ){
				SQL +=" and a.STAFF_ID like '%"+staffId.toString()+"%'"; 	
			}if(userId!=null && userId.toString().length()>0 ){
				SQL +=" and a.USER_ID like '%"+userId.toString()+"%'"; 	
			}if(realName!=null && realName.toString().length()>0 ){
				SQL +=" and b.REAL_NAME like '%"+realName.toString()+"%'"; 	
			}
		   System.out.println("sql="+SQL);
		   Map formMap = new HashMap();
	   	   SqlUtis.prepareSql(formMap,SQL);
		   return this.list(this.getStatementName("list"), formMap, page);
		}
	/**
	 * 添加用户，返回用户帐号
	 * @param user
	 * @return
	 */
	public String addRsUser(@NotNull RsUser user){
		String userId = this.userService.insert(user).toString();
		return userId;
	}
	/**
	 * 添加店员
	 * @param user
	 * @return
	 */
	public void addRsUserShop(@NotNull RsUserShop userShop){
		this.userShopService.insert(userShop);
	}
    
	public RsUser getRsUserByUserName(@RsUser.AnnoUserName String userName){
		Map formMap = new HashMap();
		formMap.put(RsUser.COL_USER_NAME, userName);
        SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a."+RsUser.COL_USER_NAME,Types.VARCHAR,SqlUtis.EQ));
        return this.userService.one(formMap);
	}
	/**
	 * 查看该用户是否已经是该代理商的店员
	 * @param userId
	 * @param shopId
	 * @return
	 */
	public boolean exitsByShopIdUserId(@RsUserShop.AnnoUserId String userId,@RsUserShop.AnnoShopId String shopId){
		Map formMap = new HashMap();
		formMap.put(RsUserShop.COL_USER_ID, userId);
		formMap.put(RsUserShop.COL_SHOP_ID, shopId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap,"a."+RsUserShop.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),
				SqlUtis.getSQLRequired(formMap,"a."+RsUserShop.COL_USER_ID,Types.VARCHAR,SqlUtis.EQ));
		return this.userShopService.exists(formMap);
	}
	
	/**
	 * 查看用户是否已经成为代理商的店员
	 * @return
	 */
	public boolean exitsByUserId(String userId){
		Map formMap = new HashMap();
		formMap.put(RsUserShop.COL_USER_ID, userId);
		SqlUtis.prepareSql(formMap,SqlUtis.getSQLRequired(formMap,"a."+RsUserShop.COL_USER_ID,Types.VARCHAR,SqlUtis.EQ));
		return this.userShopService.exists(formMap);
	}
}
